package com.wjm.data_handle;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.File;
import java.util.*;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) throws DocumentException {
        StringBuffer staStr=new StringBuffer();
        StringBuffer dataStr=new StringBuffer();
        List<String> status_result_list = new ArrayList<>( );
        List<String> dataList = new ArrayList<>( );
        SAXReader reader=new SAXReader();
        status_result_list.add("name,value");
        dataList.add("wavelength,value");

        File file = new File("C:\\Users\\11044\\Desktop\\work2\\m210629-101059.xml");

        if(!file.exists()){
            System.out.println("no exists" );
        }
        Document doc = reader.read(file);
        Element root = doc.getRootElement();
        Element measurement = root.element("measurement");
        Element status = measurement.element("status");
        Element data = measurement.element("data");
        Element results=measurement.element("results");
        //parameter.csv
        for (Iterator<Element> parameters=status.elementIterator("parameter");parameters.hasNext();) {
            Element next = parameters.next( );

            status_result_list.add(next.attributeValue("caption")+","+next.getText().trim());
        }

        for (Iterator<Element> parameters=results.elementIterator("parameter");parameters.hasNext();) {
            Element next = parameters.next( );
            if(next.attributeValue("caption").equals("CVG")){
                status_result_list.add("\""+next.attributeValue("caption")+","+next.getText().trim()+"\"");
                continue;
            }
            status_result_list.add(next.attributeValue("caption")+","+next.getText().trim());
        }
        //wavelength.csv
        for (Iterator<Element> rows=data.elementIterator("row");rows.hasNext();) {

            Element next = rows.next( );
            dataList.add(next.attributeValue("wavelength")+","+next.attributeValue("value"));
        }
        for (String s:
                status_result_list) {
            staStr.append(s+"\r\n");
        }
        for (String s:
                dataList) {
            dataStr.append(s+"\r\n");
        }


        System.out.println(staStr);
    }
}
